package demo.jdbc.example.utils;

import java.sql.*;

public class JDBCUtils {

    // 加载驱动，并建立数据库连接
    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://39.108.52.113:3306/test_jdbc?" +
                    "useSSL=false&&allowPublicKeyRetrieval=true&serverTimezone=UTC";
        String userName = "root";
        String password = "Aa123-456";
        Connection connection = DriverManager.getConnection(url, userName, password);
        return connection;


    }
    // 关闭数据库连接，释放资源
    public static void release(Statement statement, Connection connection){
        if (statement != null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            statement = null;
        }
        if (connection != null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            connection = null;
        }
    }
    public static void release(ResultSet resultSet, Statement statement, Connection connection){
        if (resultSet != null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            resultSet = null;
        }
        release(statement, connection);
    }
}
